<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="支持代码" prop="technicalSupportCode">
        <el-input
          v-model="queryParams.technicalSupportCode"
          placeholder="请输入技术支持代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支持标题" prop="technicalSupportTitle">
        <el-input
          v-model="queryParams.technicalSupportTitle"
          placeholder="请输入技术支持标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.after_support_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="重要等级" prop="importantLevel">
        <el-select v-model="queryParams.importantLevel" placeholder="请选择重要等级" clearable>
          <el-option
            v-for="dict in dict.type.support_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工单id" prop="repairOrderId">
        <el-input
          v-model="queryParams.repairOrderId"
          placeholder="请输入维修工单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商id" prop="dealerId">
        <el-input
          v-model="queryParams.dealerId"
          placeholder="请输入经销商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商名" prop="dealerName">
        <el-input
          v-model="queryParams.dealerName"
          placeholder="请输入经销商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报人名" prop="reportByName">
        <el-input
          v-model="queryParams.reportByName"
          placeholder="请输入上报人名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报人id" prop="reportBy">
        <el-input
          v-model="queryParams.reportBy"
          placeholder="请输入上报人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报时间" prop="reportAt">
        <el-date-picker clearable
          v-model="queryParams.reportAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择上报时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="回复人名" prop="replyByName">
        <el-input
          v-model="queryParams.replyByName"
          placeholder="请输入回复人名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回复人id" prop="replyBy">
        <el-input
          v-model="queryParams.replyBy"
          placeholder="请输入回复人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回复时间" prop="replyAt">
        <el-date-picker clearable
          v-model="queryParams.replyAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择回复时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完结人名" prop="finishByName">
        <el-input
          v-model="queryParams.finishByName"
          placeholder="请输入完结人名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="完结人id" prop="finishBy">
        <el-input
          v-model="queryParams.finishBy"
          placeholder="请输入完结人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="完结时间" prop="finishAt">
        <el-date-picker clearable
          v-model="queryParams.finishAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择完结时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核人id" prop="auditorId">
        <el-input
          v-model="queryParams.auditorId"
          placeholder="请输入审核人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人名" prop="auditorName">
        <el-input
          v-model="queryParams.auditorName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditorTime">
        <el-date-picker clearable
          v-model="queryParams.auditorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['aftersale:support:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['aftersale:support:edit']"
        >回复</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['aftersale:support:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['aftersale:support:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="技术支持代码" align="center" prop="technicalSupportCode" />
      <el-table-column label="技术支持标题" align="center" prop="technicalSupportTitle" />
      <el-table-column label="技术支持描述" align="center" prop="technicalSupportDesc" />
      <el-table-column label="回复内容" align="center" prop="replyContent" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.after_support_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="重要等级" align="center" prop="importantLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.support_level" :value="scope.row.importantLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="维修工单id" align="center" prop="repairOrderId" />
      <el-table-column label="经销商id" align="center" prop="dealerId" />
      <el-table-column label="经销商名称" align="center" prop="dealerName" />
      <el-table-column label="上报人名" align="center" prop="reportByName" />
      <el-table-column label="上报人id" align="center" prop="reportBy" />
      <el-table-column label="上报时间" align="center" prop="reportAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回复人名" align="center" prop="replyByName" />
      <el-table-column label="回复人id" align="center" prop="replyBy" />
      <el-table-column label="回复时间" align="center" prop="replyAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.replyAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完结人名" align="center" prop="finishByName" />
      <el-table-column label="完结人id" align="center" prop="finishBy" />
      <el-table-column label="完结时间" align="center" prop="finishAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人id" align="center" prop="auditorId" />
      <el-table-column label="审核人名称" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aftersale:support:edit']"
          >回复</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['aftersale:support:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改技术支援管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="技术支持代码" prop="technicalSupportCode">
          <el-input v-model="form.technicalSupportCode" placeholder="请输入技术支持代码（若不输入，后台自动生成）" :disabled="true"/>
        </el-form-item>
        <el-form-item label="技术支持标题" prop="technicalSupportTitle">
          <el-input v-model="form.technicalSupportTitle" placeholder="请输入技术支持标题":disabled="true" />
        </el-form-item>
        <el-form-item label="技术支持描述" prop="technicalSupportDesc">
          <el-input v-model="form.technicalSupportDesc" type="textarea" placeholder="请输入内容" :disabled="true"/>
        </el-form-item>
        <el-form-item label="回复内容" prop="replyContent">
          <el-input v-model="form.replyContent" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.after_support_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重要等级" prop="importantLevel">
          <el-select v-model="form.importantLevel" placeholder="请选择重要等级">
            <el-option
              v-for="dict in dict.type.support_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
              :disabled="true"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="维修工单id" prop="repairOrderId">-->
<!--          <el-input v-model="form.repairOrderId" placeholder="请输入维修工单id" />-->
<!--        </el-form-item>-->
        <!--  查找维修工单  -->
        <el-table v-loading="loading" :data="repairOrderList" @selection-change="handleSelectionChange01">
          <el-table-column v-if="title=='添加技术支援管理'" type="selection" width="55" align="center" />
          <el-table-column label="工单ID" align="center" prop="id" />
          <el-table-column label="经销商ID" align="center" prop="dealerId" />
          <el-table-column label="经销商名称" align="center" prop="dealerName" />
          <el-table-column label="预约单号" align="center" prop="reservationNo" />
          <el-table-column label="工单编号" align="center" prop="roNo" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.repair_order_status" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="品牌名称" align="center" prop="brandName">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.car_brand" :value="scope.row.brandName"/>
            </template>
          </el-table-column>
          <el-table-column label="工单类型" align="center" prop="workOrderType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.repair_order_type" :value="scope.row.workOrderType"/>
            </template>
          </el-table-column>
          <el-table-column label="维修工人" align="center" prop="maintainWorker">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.repair_order_worker" :value="scope.row.maintainWorker"/>
            </template>
          </el-table-column>
          <el-table-column label="进厂时间" align="center" prop="incomingTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.incomingTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="进厂里程" align="center" prop="inMileage" />
          <el-table-column label="进厂油位" align="center" prop="incomingOilLevel" />
          <el-table-column label="是否洗车" align="center" prop="isWash">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.no_and_yes" :value="scope.row.isWash"/>
            </template>
          </el-table-column>
          <el-table-column label="是否保养" align="center" prop="isMaintain">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.no_and_yes" :value="scope.row.isMaintain"/>
            </template>
          </el-table-column>
          <el-table-column label="工时单价" align="center" prop="labourPrice" />
          <el-table-column label="总金额" align="center" prop="totalAmount" />
          <el-table-column label="理赔单号" align="center" prop="insurantionNo" />
          <el-table-column label="保险公司" align="center" prop="insurantionName">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.repair_insurantion_company" :value="scope.row.insurantionName"/>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="审核人id" align="center" prop="auditorId" />
          <el-table-column label="审核人名称" align="center" prop="auditorName" />
          <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupport, getSupport, delSupport, addSupport, updateSupport } from "@/api/aftersale/supportReply";
import {getRepairOrder, listRepairOrder} from "@/api/aftersale/repairOrder"

export default {
  name: "Support",
  dicts: ['after_support_status', 'support_level'],
  data() {
    return {
      //获取到技术支援单的工单id
      orderIdTemp : null,
      //查询到的维修工单列表
      repairOrderList:[],
      //选中的那个维修工单id
      orderIds:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 技术支援管理表格数据
      supportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        technicalSupportCode: null,
        technicalSupportTitle: null,
        technicalSupportDesc: null,
        replyContent: null,
        status: null,
        importantLevel: null,
        repairOrderId: null,
        dealerId: null,
        dealerName: null,
        reportByName: null,
        reportBy: null,
        reportAt: null,
        replyByName: null,
        replyBy: null,
        replyAt: null,
        finishByName: null,
        finishBy: null,
        finishAt: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // technicalSupportCode: [
        //   { required: true, message: "技术支持代码不能为空", trigger: "blur" }
        // ],
        technicalSupportTitle: [
          { required: true, message: "技术支持标题不能为空", trigger: "blur" }
        ],
        technicalSupportDesc: [
          { required: true, message: "技术支持描述不能为空", trigger: "blur" }
        ],
        // replyContent: [
        //   { required: true, message: "回复内容不能为空", trigger: "blur" }
        // ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        importantLevel: [
          { required: true, message: "重要等级不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询技术支援管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.status = 1;
      listSupport(this.queryParams).then(response => {
        this.supportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.repairOrderList=[];
      // 查询参数
      this.queryParams= {
        pageNum: 1,
        pageSize: 10,
        id: null,
        technicalSupportCode: null,
        technicalSupportTitle: null,
        technicalSupportDesc: null,
        replyContent: null,
        status: null,
        importantLevel: null,
        repairOrderId: null,
        dealerId: null,
        dealerName: null,
        reportByName: null,
        reportBy: null,
        reportAt: null,
        replyByName: null,
        replyBy: null,
        replyAt: null,
        finishByName: null,
        finishBy: null,
        finishAt: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
      };
      this.form = {
        id: null,
        technicalSupportCode: null,
        technicalSupportTitle: null,
        technicalSupportDesc: null,
        replyContent: null,
        status: null,
        importantLevel: null,
        repairOrderId: null,
        dealerId: null,
        dealerName: null,
        reportByName: null,
        reportBy: null,
        reportAt: null,
        replyByName: null,
        replyBy: null,
        replyAt: null,
        finishByName: null,
        finishBy: null,
        finishAt: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleSelectionChange01(selection) {
      this.orderIds = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加技术支援管理";
      //查询工单
      this.getrepairOrderList();
    },
    getrepairOrderList() {
      this.loading = true;
      // this.queryParams.status=2;
      listRepairOrder(this.queryParams).then(response => {
        this.repairOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSupport(id).then(response => {
        this.form = response.data;
        this.orderIdTemp = response.data.repairOrderId;
        this.open = true;
        this.title = "修改技术支援管理";
        //根据工单id，查询工单信息
        this.getrepairOrderListById(this.orderIdTemp);
      });
    },
    getrepairOrderListById(orderIdTemp){
      this.loading = true;
      this.queryParams.id = orderIdTemp;
      listRepairOrder(this.queryParams).then(response => {
        this.repairOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.queryParams.id = null;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSupport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.orderIds)
            if (this.orderIds.length != 1){
              this.$message.error('单次必须且只能选择一个工单号！');
              return;
            }
            this.form.repairOrderId = this.orderIds[0];
            addSupport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除技术支援管理编号为"' + ids + '"的数据项？').then(function() {
        return delSupport(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('aftersale/support/export', {
        ...this.queryParams
      }, `support_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
